<?php

Yii::import('application.extensions.Api._base.BaseApiFunctionality');

class userApiFunctionality extends BaseApiFunctionality {
    /* --------------- Start  GET Actions --------------------- */

    /* --------------- End GET Actions ------------------------ */


    /* --------------- Start POST Actions --------------------- */

    public function registration() {
        if ($this->validPostRequest()) {
            $username = '';
            $password = '';
            if ($_POST['user_name'] != '') {
                $username = $_POST['user_name'];
            }

            if ($_POST['password'] != '') {
                $password = $_POST['password'];
            }


            if (!empty($username) && !empty($password)) {

                $dbUser = Users::model()->find('username=:username', array(':username' => trim($username)));
                if ($dbUser) {
                    $data = new ECOMResult(501, 'Username ' . $username . ' already registered. Please try again with different user name.');
                } else {
                    $transaction = Yii::app()->getDb()->beginTransaction();   //dbConnection->beginTransaction();
                    try {
                        $createtime = time();
                        $lastvisit = time();
                        $user = new Users();
                        $user->setAttribute('username', trim($username));
                        $user->setAttribute('password', md5($password));
                        $user->setAttribute('createtime', $createtime);
                        $user->setAttribute('lastvisit', $lastvisit);
                        $user->setAttribute('status', 1);
                        $user->save(false);
                        $user->addProfile($_POST);
                        $user->addUserRole(2);
                        $user->saveAppUsersPreferences();
                        $email = $_POST['email'];
                        $msgTpl = 'registration_success.php';
                        $urlPath = 'http://' . Yii::app()->request->getServerName() . Yii::app()->baseUrl;
                        $body = populateEmailTemplate($msgTpl, array('$NAME' => $username, '$PATH' => $urlPath,
                            '$USERNAME' => $username,
                            '$PASSWORD' => $password));

                        anotherEmailSend($email, "Thanks for registering with us", $body);
                        
                        $data = new ECOMResult(200, 'User has been successfully registered. Thank you for registering.');
                        $transaction->commit();
                    } catch (Exception $e) {
                        $transaction->rollBack();
                        $data = $e;
                    }
                }
            } else {
                $data = new ECOMResult(501, 'Username and Password were empty. Please send valid values and try again.');
            }
        } else {
            $data = new ECOMResult(501, 'Invalid request parameters. Please check settings and try again.');
        }
        ECOMApiResponder::Respond($data, date('m-d-Y'));
    }

    /* --------------- End POST Actions --------------------- */
}

?>
